import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import { fetchValidation } from '@/utils';
import {
  queryWithdrawalMoneyList,
  operWithdrawMoneyPayWay,
  operWithdrawMoneyAlipay,
  sendPcode,
} from './service';

import { TableListData } from './data';

export interface StateType {
  datalist: TableListData;
  withdrawalInfo: any;
  withdrawalState: boolean;
  subLoading: boolean;
}

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & {
    select: <T>(func: (state: StateType) => T) => T;
  }
) => void;

export interface ModelType {
  namespace: string;
  state: StateType;
  effects: {
    fetch: Effect;
    payWay: Effect;
    alipay: Effect;
    vcode: Effect;
  };
  reducers: {
    save: Reducer;
    change: Reducer;
    status: Reducer;
    loading: Reducer;
    changeLoading: Reducer;
    offModal: Reducer;
    clear: Reducer;
  };
}

const Model: ModelType = {
  namespace: 'withdrawalMoney',

  state: {
    datalist: {
      list: [],
      count: 0,
    },
    withdrawalInfo: {
      fcs_id: '',
      fcs_name: '',
      income: 0,
      withdraw_charge_ratio: 0,
      currentOper: 1,
      withdrawalResults: false,
    },
    withdrawalState: false,
    subLoading: false,
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryWithdrawalMoneyList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: response.body,
      });
    },

    * payWay({ payload }, { call, put }) {
      const response = yield call(operWithdrawMoneyPayWay, payload);
      const data = fetchValidation(response);
      if (data) {
        response.body.withdrawalResults = true;
        yield put({
          type: 'change',
          payload: response.body,
        });
        yield put({ type: 'offModal' });
      }
      return response;
    },

    * alipay({ payload: { searchValues, ...query } }, { call, put }) {
      const response = yield call(operWithdrawMoneyAlipay, query);
      if (response) {
        if (response.code === 0) {
          message.success('提现成功');
          yield put({
            type: 'status',
            payload: true,
          });
          yield put({
            type: 'fetch',
            payload: searchValues,
          });
        } else {
          message.error(response.msg);
          yield put({
            type: 'loading',
            payload: true,
          });
          yield put({
            type: 'changeLoading',
            payload: false,
          });
        }
      }
    },

    * vcode({ payload }, { call, put }) {
      const response = yield call(sendPcode, payload);
      const data = fetchValidation(response);
      if (data) {
        message.success('验证码发送成功');
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        datalist: action.payload,
      };
    },

    change(state, { payload }) {
      return {
        ...state,
        withdrawalInfo: payload,
      };
    },

    status(state, { payload }) {
      return {
        ...state,
        withdrawalState: payload,
      };
    },

    loading(state, { payload }) {
      return {
        ...state,
        subLoading: payload,
      };
    },

    changeLoading(state, { payload }) {
      return {
        ...state,
        subLoading: payload,
      };
    },

    offModal(state) {
      return {
        ...state,
        withdrawalState: false,
      };
    },

    clear(state) {
      return {
        ...state,
        withdrawalInfo: {
          fcs_id: '',
          fcs_name: '',
          income: 0,
          withdraw_charge_ratio: 0,
          currentOper: 1,
          withdrawalResults: false,
        },
      };
    },
  },
};

export default Model;
